import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:sui_shou_pai/Utils/storage_util.dart';
import 'package:sui_shou_pai/network/http_config.dart';
import 'package:sui_shou_pai/network/http_request.dart';
import 'package:sui_shou_pai/record/record_card_list.dart';
import 'package:sui_shou_pai/record/record_entity/photo_record_entity.dart';

class RecordCardListAll extends StatefulWidget {
  const RecordCardListAll({Key key}) : super(key: key);

  @override
  _RecordCardListAllState createState() => _RecordCardListAllState();

}


class _RecordCardListAllState extends State<RecordCardListAll> {
  List<PhotoRecordEntity> photoRecordEntityList = [];
  List<PhotoRecordEntity> plist = [];

 // Function  callback;

  Future<void> getData() async {
    plist.clear();
    HttpRequest.request("$BASE_URL/photo-record/getsortPhotoRecordList",
            method: "GET", token: await StorageUtil.getStringItem("token"))
        .then((value) {
      var data = jsonDecode(value.toString());
      List<dynamic> res = data['result'];
      for (int i = 0; i < res.length; i++) {
        PhotoRecordEntity photoRecordEntity =
            PhotoRecordEntity.fromJson(res[i]);
        plist.add(photoRecordEntity);
      }
      setState(() {
        photoRecordEntityList.clear();
        if (plist.isNotEmpty) {
          for (int i = 0; i < plist.length; i++) {
            photoRecordEntityList.add(plist[i]);
          }
        }
      });
    });
  }


  @override
  void initState() {
    super.initState();
    getData();
  }

  @override
  Widget build(BuildContext context) {


    return RefreshIndicator(
        onRefresh: onRefresh,
        child: RecordCardList(photoRecordEntityList: photoRecordEntityList
        ));
  }

  // 下拉刷新
  Future<void> onRefresh() async {
    setState(() {
      getData();
    });
    Fluttertoast.showToast(
      msg: "刷新成功",
      toastLength: Toast.LENGTH_SHORT,
      gravity: ToastGravity.BOTTOM,
    );
  }
}
